# Contributor: John Coyle <dx9err@gmail.com>
# Contributor: Iggy Jackson <iggy@kws1.com>
# Contributor: Duncan Bellamy <dunk@denkimushi.com>
# Maintainer: Duncan Bellamy <dunk@denkimushi.com>
pkgname=ceph
pkgver=16.2.9
pkgrel=2
pkgdesc="Ceph is a distributed object store and file system"
pkgusers="ceph"
pkggroups="ceph"
url="https://ceph.io/en/"
# riscv64 missing nodejs
arch="all !riscv64"
# https://github.com/ceph/ceph/blob/master/COPYING
license="LGPL-2.1-only AND LGPL-2.0-or-later AND GPL-2.0-only AND GPL-3.0-only AND CC-BY-SA-1.0 AND BSL-1.0 AND GPL-2.0-or-later WITH Autoconf-exception-2.0 AND BSD-3-Clause AND MIT AND custom"
depends="ceph-osd ceph-mds ceph-mon"
_lua=5.3
_base_deps="
	cryptsetup
	e2fsprogs
	logrotate
	parted
	util-linux
	xfsprogs
	"
_osd_daemon_deps="fuse snappy lz4-libs"
_osd_tools_deps="lz4-libs"
_ceph_volume_deps="lvm2"
_ceph_test_deps="
	xmlstarlet
	py3-coverage
	py3-flake8
	py3-nodeenv
	py3-nose
	py3-pytest
	py3-tox
	py3-yaml
	"
makedepends="
	acl-dev
	argp-standalone
	bc
	boost-dev
	btrfs-progs
	bzip2-dev
	cmake
	coreutils
	cryptsetup-dev
	cunit-dev
	curl-dev
	cython
	diffutils
	doxygen
	eudev-dev
	expat-dev
	fcgi-dev
	flex
	fmt-dev
	fuse
	fuse-dev
	git
	graphviz-dev
	grep
	gperf
	jq
	keyutils-dev
	leveldb-dev
	libaio-dev
	libatomic_ops-dev
	libcap-ng-dev
	libedit-dev
	librdkafka-dev
	libnl3-dev
	libtirpc-dev
	libtool
	libxml2-dev
	linux-headers
	lua$_lua-dev
	lvm2-dev
	lz4-dev
	nodejs
	npm
	nss-dev
	oath-toolkit-dev
	openldap-dev
	openssl1.1-compat-dev
	procps
	python3-dev
	py3-prettytable
	py3-sphinx
	rabbitmq-c-dev
	readline-dev
	rpcgen
	samurai
	snappy-dev
	sqlite-dev
	userspace-rcu-dev
	xfsprogs-dev
	xmlstarlet
	yarn
	yasm
	$_base_deps
	$_osd_daemon_deps
	$_osd_tools_deps
	$_ceph_volume_deps
	$_ceph_test_deps
	"

source="https://download.ceph.com/tarballs/ceph_$pkgver.orig.tar.gz
	ceph.confd
	ceph.initd
	10-musl-fixes.patch
	11-dump_time_header_impl.patch
	11-parse_rfc1123_alt.patch
	11-s3_expiration_header.patch
	12-package.json-resolutions.patch
	20-pci.patch
	30-32bit_fix.patch.noauto
	30-cypress.patch.noauto
	30-ubuntu-32bit-fixes.patch.noauto
	31-32bit_fix_tests.patch.noauto
	32-upstream32bit.patch
	32-upstream32bitcleanup.patch
	35-fix_ErasureCodeShec.patch
	37-fix_tests.patch
	42-no-virtualenvs.patch
	43-LogClock.h.patch
	44-aarch64-erasure.patch
	44-cmake-buildtype.patch
	44-staticcast.patch
	44-missing-include.patch
	45-python.patch
	"

subpackages="
	$pkgname-doc
	$pkgname-base
	$pkgname-common
	$pkgname-mds
	$pkgname-mgr
	$pkgname-mon::noarch
	$pkgname-mon-daemon:mon_daemon
	$pkgname-mon-tools:mon_tools
	$pkgname-fuse:ceph_fuse
	$pkgname-openrc
	$pkgname-osd::noarch
	$pkgname-osd-daemon:osd_daemon
	$pkgname-osd-tools:osd_tools
	$pkgname-volume:ceph_volume:noarch
	$pkgname-radosgw
	$pkgname-bash-completion:bash_completion:noarch
	$pkgname-dev
	$pkgname-user::noarch
	$pkgname-utils
	rbd-fuse:rbd_fuse
	rbd-mirror:rbd_mirror
	rbd-nbd:rbd_nbd
	librbd
	libcephfs
	librados
	py3-rados:_py3_rados
	libradosstriper
	py3-rbd:_py3_rbd
	py3-cephfs:_py3_cephfs
	"

# secfixes:
#   16.2.4-r0:
#     - CVE-2021-3509
#     - CVE-2021-3531
#     - CVE-2021-3524
#   16.2.3-r0:
#     - CVE-2021-20288
#   15.2.8-r0:
#     - CVE-2020-27781
#   15.2.6-r0:
#     - CVE-2020-25660
#     - CVE-2020-10736
#     - CVE-2020-10753
#   14.2.9-r0:
#     - CVE-2020-1759
#     - CVE-2020-1760
#   14.2.7-r0:
#     - CVE-2020-1699
#     - CVE-2020-1700
#   14.2.3-r0:
#     - CVE-2019-10222

_ceph_uid=167
_ceph_gid=167

_py3_sitelib() {
	python3 -c "import site; print(site.getsitepackages()[0])"
}

prepare() {
	default_prepare

	case "$CARCH" in
		x86|armv7|armhf)
			patch -p1 -i "$srcdir"/30-32bit_fix.patch.noauto
			patch -p1 -i "$srcdir"/30-cypress.patch.noauto
			patch -p1 -i "$srcdir"/30-ubuntu-32bit-fixes.patch.noauto
			patch -p1 -i "$srcdir"/31-32bit_fix_tests.patch.noauto ;;
	esac

	# delete bundled boost as >300mb and using system boost
	rm -rf src/boost
}

build() {
	export CEPH_BUILD_VIRTUALENV="$builddir"

	# builders keep failing when -jN == nproc
	export MAKEFLAGS="$MAKEFLAGS -j$((JOBS<16 ? JOBS : 16))"

	# use alternate registry as original can timeout for arm32bit
	export NPM_REGISTRY=https://registry.npmjs.org

	cmake -B build -G Ninja \
		-DCMAKE_BUILD_TYPE=MinSizeRel \
		-DLUA_LIBRARIES=/usr/lib/lua"$_lua"/liblua.so \
		-DALLOCATOR=libc \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_LIBDIR=/usr/lib \
		-DCMAKE_INSTALL_LOCALSTATEDIR=/var \
		-DCMAKE_INSTALL_SYSCONFDIR=/etc \
		-DWITH_REENTRANT_STRSIGNAL=ON \
		-DWITH_THREAD_SAFE_RES_QUERY=ON \
		-DWITH_MANPAGE=ON \
		-DWITH_SYSTEM_BOOST=ON \
		-DWITH_SYSTEM_NPM=ON \
		-DWITH_PYTHON3=3 \
		-DWITH_LTTNG=OFF \
		-DWITH_RDMA=OFF \
		-DWITH_SYSTEMD=OFF \
		-DWITH_SPDK=OFF \
		-DWITH_BABELTRACE=OFF \
		-DWITH_RADOSGW_AMQP_ENDPOINT=OFF \
		-DWITH_TESTS=OFF
	cmake --build build

}

package() {
	# free up some space before install
	rm -rf build/src/pybind/mgr/dashboard/cypress
	rm -rf src/pybind/mgr/dashboard/frontend/node_modules

	DESTDIR="$pkgdir" cmake --install build
	# yarn creates an empty usr/local/bin
	rm -rf "${pkgdir:?}"/usr/local

	# fix /usr permission
	chmod 755 "$pkgdir/usr"

	# remove dashboard angular app source
	rm -rf "$pkgdir"/usr/share/ceph/mgr/dashboard/frontend/src

	# remove the upstream init file and put in openrc ones
	rm -f "$pkgdir"/etc/init.d/ceph
	install -D -m 755 "$srcdir"/"$pkgname".initd "$pkgdir"/etc/init.d/ceph
	install -D -m 644 "$srcdir"/"$pkgname".confd "$pkgdir"/etc/conf.d/ceph

	# move mount.* binaries to /sbin
	mkdir -p "$pkgdir"/sbin
	mv "$pkgdir"/usr/sbin/mount.* "$pkgdir"/sbin

	install -m 644 -D src/etc-rbdmap "$pkgdir"/etc/ceph/rbdmap
	install -m 644 -D src/logrotate.conf "$pkgdir"/etc/logrotate.d/ceph
	install -m 644 -D etc/sysctl/90-ceph-osd.conf "$pkgdir"/etc/sysctl.d/90-ceph-osd.conf

	# udev rules
	install -m 644 -D udev/50-rbd.rules "$pkgdir"/etc/udev/rules.d/50-rbd.rules
	# sudoers.d
	install -m 600 -D sudoers.d/ceph-smartctl "$pkgdir"/etc/sudoers.d/ceph-smartctl

	# delete systemd related stuff
	rm "$pkgdir"/usr/sbin/ceph-volume-systemd

	# move docs to docs
	mkdir -p "$pkgdir"/usr/share/doc/ceph/dashboard
	mv "$pkgdir"/usr/share/ceph/mgr/dashboard/*.rst "$pkgdir"/usr/share/doc/ceph/dashboard/
	mv "$pkgdir"/usr/share/ceph/mgr/cephadm/HACKING.rst "$pkgdir"/usr/share/doc/ceph/cephadm-HACKING.rst
}

check() {
	cd "$builddir"/build
	ctest
}

# This subpackage is a little weird and a side effect of trying to keep
# the daemon packages as small as possible. We might be able to roll this into
# the base/common package later if we can slim those down or break them up in a
# different way
user() {
	pkgdesc="Minimal package to create a user for other ceph packages."
	depends=""
	install="$pkgname-user.pre-install"
	mkdir -p "$subpkgdir"
}

base() {
	pkgdesc="Base is the package that includes all the files shared amongst ceph servers"
	depends="$_base_deps
		$pkgname-user=$pkgver-r$pkgrel
		$pkgname-common=$pkgver-r$pkgrel
		librbd=$pkgver-r$pkgrel
		librados=$pkgver-r$pkgrel
		libcephfs=$pkgver-r$pkgrel
		"

	amove usr/bin/ceph-crash \
		usr/bin/crushtool \
		usr/bin/*maptool \
		usr/bin/ceph-kvstore-tool \
		usr/bin/ceph-run \
		usr/sbin/ceph-create-keys \
		usr/libexec/ceph/ceph_common.sh \
		usr/lib/rados-classes/*.so* \
		usr/lib/ceph/erasure-code/libec_*.so* \
		usr/lib/ceph/compressor/libceph_*.so* \
		usr/lib/ceph/crypto/libceph_*.so* \
		etc/logrotate.d/ceph
	for dir in crash crash/posted tmp bootstrap-osd bootstrap-mds \
			bootstrap-rgw bootstrap-mgr bootstrap-rbd \
			bootstrap-rbd-mirror; do

		install -m 750 -o $_ceph_uid -g $_ceph_gid -d \
			"$subpkgdir"/var/lib/ceph/$dir
	done
}

common() {
	pkgdesc="Common utilities to mount and interact with a ceph storage cluster."
	depends="py3-rados py3-rbd py3-cephfs py3-prettytable"

	amove usr/bin/ceph \
		usr/bin/ceph-authtool \
		usr/bin/ceph-conf \
		usr/bin/ceph-rbdnamer \
		usr/bin/rados \
		usr/bin/rbd \
		usr/bin/rbdmap \
		sbin/mount.ceph \
		usr/share/ceph/*.ceph.com* \
		etc/ceph/rbdmap

	amove "$(_py3_sitelib)"/ceph_argparse.py* "$(_py3_sitelib)"/ceph_daemon.py*

	amove etc/udev/rules.d/50-rbd.rules
	install -m 3770 -o $_ceph_uid -g $_ceph_gid -d \
		"$subpkgdir"/var/log/ceph
	install -m 750 -o $_ceph_uid -g $_ceph_gid -d \
		"$subpkgdir"/var/lib/ceph
}

mds() {
	pkgdesc="Metadata server daemon for the Ceph distributed file system."
	depends="ceph-base"

	amove usr/bin/ceph-mds
	install -m 750 -o $_ceph_uid -g $_ceph_gid -d \
		"$subpkgdir"/var/lib/ceph/mds
}

mon() {
	pkgdesc="Cluster monitor daemon virtual package for the Ceph distributed file system."
	depends="$pkgname-base
		$pkgname-mon-daemon=$pkgver-r$pkgrel
		$pkgname-mon-tools=$pkgver-r$pkgrel"

	mkdir -p "$subpkgdir"
}

mon_daemon() {
	pkgdesc="Cluster monitor daemon for the Ceph distributed file system."
	depends="$pkgname-user=$pkgver-r$pkgrel"

	amove usr/bin/ceph-mon
	install -m 750 -o $_ceph_uid -g $_ceph_gid -d \
		"$subpkgdir"/var/lib/ceph/mon
}

mon_tools() {
	pkgdesc="Cluster monitor tools for the Ceph distributed file system."
	depends=

	amove usr/bin/ceph-monstore-tool
	install -m 750 -o $_ceph_uid -g $_ceph_gid -d \
		"$subpkgdir"/var/lib/ceph/mon
}

ceph_fuse() {
	pkgdesc="FUSE based client for Ceph distributed network file system."
	depends=

	amove usr/bin/ceph-fuse sbin/mount.fuse.ceph
}

rbd_fuse() {
	pkgdesc="FUSE based client to map Ceph rbd images to files."
	depends=

	amove usr/bin/rbd-fuse
}

rbd_mirror() {
	pkgdesc="Daemon for mirroring RBD images between Ceph clusters."
	depends="ceph-common=$pkgver-r$pkgrel"

	amove usr/bin/rbd-mirror
}

rbd_nbd() {
	pkgdesc="NBD based client to map Ceph rbd images to local device."
	depends=

	amove usr/bin/rbd-nbd
}

radosgw() {
	pkgdesc="Rados REST gateway which implements Amazon's S3 and OpenStack's Swift APIs."
	depends="ceph-common oath-toolkit-liboath"

	amove usr/bin/radosgw*
	mkdir -p "$subpkgdir"/var/lib/ceph/radosgw
}

osd() {
	pkgdesc="Object storage daemon virtual package for the Ceph distributed file system."
	depends="$pkgname-osd-daemon=$pkgver-r$pkgrel
		$pkgname-osd-tools=$pkgver-r$pkgrel
		$pkgname-volume=$pkgver-r$pkgrel"

	mkdir -p "$subpkgdir"
}

osd_daemon() {
	pkgdesc="Object storage daemon for the Ceph distributed file system."
	depends="$_osd_daemon_deps $pkgname-user=$pkgver-r$pkgrel"

	amove usr/bin/ceph-osd \
		usr/libexec/ceph/ceph-osd-prestart.sh \
		etc/sudoers.d/ceph-smartctl \
		etc/sysctl.d/90-ceph-osd.conf
	install -m 750 -o $_ceph_uid -g $_ceph_gid -d \
		"$subpkgdir"/var/lib/ceph/osd
}

osd_tools() {
	pkgdesc="Object storage daemon tools for the Ceph distributed file system."
	depends="$pkgname-base=$pkgver-r$pkgrel $_osd_tools_deps"

	amove usr/bin/ceph-clsinfo usr/bin/ceph-*-tool
	install -m 750 -o $_ceph_uid -g $_ceph_gid -d \
		"$subpkgdir"/var/lib/ceph/osd
}

ceph_volume() {
	pkgdesc="ceph-volume tool for the Ceph distributed file system."
	depends="$_ceph_volume_deps"

	amove usr/sbin/ceph-volume \
		"$(_py3_sitelib)"/ceph_volume "$(_py3_sitelib)"/ceph_volume-*
}

librados() {
	pkgdesc="RADOS distributed object store client library"
	depends=

	amove usr/lib/librados.so.* usr/lib/ceph/libceph-common.so.*
}

_py3_rados() {
	pkgdesc="Python libraries for the RADOS object store"
	depends=

	amove "$(_py3_sitelib)"/rados*
}

libradosstriper() {
	pkgdesc="RADOS striping library"
	depends=

	amove usr/lib/libradosstriper.so.*
}

librbd() {
	pkgdesc="RADOS block device client library"
	depends=

	amove usr/lib/librbd.so.*
}

_py3_rbd() {
	pkgdesc="Python libraries for the RADOS block device"
	depends="py3-rados"

	amove "$(_py3_sitelib)"/rbd*
}

libcephfs() {
	pkgdesc="Ceph distributed file system client library"
	depends=

	amove usr/lib/libcephfs.so.*
}

_py3_cephfs() {
	pkgdesc="Python libraries for Ceph distributed file system"
	depends="py3-rados"

	amove "$(_py3_sitelib)"/cephfs* "$(_py3_sitelib)"/ceph_volume_client.py
}

utils() {
	pkgdesc="Ceph utils and recovery tools"
	depends="ceph-common=$pkgver-r$pkgrel"

	amove usr/bin/ceph-diff-sorted \
		usr/bin/ceph-post-file \
		usr/bin/ceph-syn \
		usr/bin/cephfs-data-scan \
		usr/bin/ceph-dencoder \
		usr/bin/cephfs-journal-tool \
		usr/bin/cephfs-table-tool \
		usr/bin/rbd-replay*
}

bash_completion() {
	pkgdesc="Bash completions for Ceph"
	depends=

	install_if="ceph=$pkgver-r$pkgrel bash-completion"
	amove etc/bash_completion.d/*
}

mgr() {
	pkgdesc="Ceph Manager Daemon"
	depends="ceph-base=$pkgver-r$pkgrel"

	amove usr/bin/ceph-mgr usr/share/ceph/mgr

	install -m 750 -o $_ceph_uid -g $_ceph_gid -d \
		"$subpkgdir"/var/lib/ceph/mgr
}

sha512sums="
bf369038ad6383f88abbed267f0239e9d8a1d33b46b34ed260aeeb1fda1b374cb6e658238e1ace013fbbb68e73b8cd2922b7def318d7a110e5f4560eeae8fca3  ceph_16.2.9.orig.tar.gz
110bdbcb40216c7ed155a8d23020784741b4992d895f4f04a146d275506e4e68053854d3b063b41e9c9b3e3e4f95b6b90602f92c185c853c0d8f47ad0c6b7121  ceph.confd
ce5f162501f6b67fe254546dddf880d1a5b1d1a0fa69e0b1918de17e8da45c5c6124512b8cbd98b76f29d931403de0d11c5ffd330ed8ee1f4dc75bb04baecae3  ceph.initd
94687578411bf0de1b17555ed81c188c88ea63ac4a25993bd8fde9cf68afbbfbdec7b2d7c54fdcfbd4aed4eb78754e8061c308955596cbe037ff15b575874cc6  10-musl-fixes.patch
211e6f5f8fc962878196ec917984781eb4a8a9495bcc8d1729b34ac66abd2d4a0b7388ae2caee9d5deb664d54860d120a37215924625e82eac9bfca16179667a  11-dump_time_header_impl.patch
17afbb87e11014acbd67c24afbc178cfa4e14776c1160cecaa03b4e933bab961d13ce9fb1924097dd8f544e3cd5ad84a844c7d3b54aa939ea63a13924369e1e0  11-parse_rfc1123_alt.patch
52b11dfd157dfb7363d2d3428901559189263caaa1c5f29a924b9be7ea012d4f54a887b22d2e2a3d756f6fd771f626505912dca52e6b19a56e018be45b7acb8b  11-s3_expiration_header.patch
9d985d19fc70f24179532831b4b19a3bfc074743542cf8d9badd60c21b02bfefbafd12b7c8530d79d9f185c7213580de19cc1e28ab4c2338b0faa52cf4acdd25  12-package.json-resolutions.patch
03ef3598181c45ecba5600a1e4db7fd897ea9d3c8abdfaad2dcf84c7a241d9ba18e7f7885d69ee0572ee307fc94600a2784a07da909d37a51de27f8ded2e3a70  20-pci.patch
b879993a285a771c3640c5214ff70e8e01673c0c5e23a0f88ac70db168aeb1dd2eaed12805ed5076adae5a0f596abcb9d82220c6fd97643169cb92788898a189  30-32bit_fix.patch.noauto
5574eda9f1a2f4a98defb380f78fc3a9a9e393d3043468f6af74967181fa912066a4d408ba27721f1f1f7625f4162a8e172fa259dcf36d1b9123be1875af057e  30-cypress.patch.noauto
01c446ac4528d8e12d3544f603ba32a0bf0d5a3a72f9f8f3726725f9d225ee447d2fea4ee2ddf10b36498cc01f2ecf51175dbfcc6376aac4c8002f52c6a04299  30-ubuntu-32bit-fixes.patch.noauto
f974ab36cd6fa49c1d4613203a4f2152723e4952a185dfb6349bc4ca8ee1a7a9d0477bea136c54248271de30a4e584734ba41e8ec41bf274b04074622888ae39  31-32bit_fix_tests.patch.noauto
fb69f1c886f3cc05a10dd8722429c6bdc840674113e83cf3592caf75b5d4082e5bdaea73426b28edbde724c4973775f2109a3eb4e0a84384fa8083621d463fbd  32-upstream32bit.patch
73326b47deb0967ff024731c28a956ca63800ffcbfa36c6351c157328d2036b3f8406368cc7c7e087841e144b32889f27d807f3405ac1c5a1c2daf6bd7ca5338  32-upstream32bitcleanup.patch
abb3cab9c01cc3d6d42a9858678059a8c9c43bda2649bf97b489fdc205eb10ae27f9fb7302062e0fb41f5cffddbfc2ae529130569034f373732a0a5d33aba86e  35-fix_ErasureCodeShec.patch
908169df45177240d2dcc721fe1637c521789aed56c3b3400ed3ba9c73d93634e4369258fb0e0395dc27053be26229ee8f370e445f0e69d01f551a710a29d407  37-fix_tests.patch
f9a0a041dbf10d5131964e0e479b47a7205bd6973335a478357861dd6651dfb79451c8cf027f71522b059765a07b842c3a71f23950134fdb4184e3f3821cfcd0  42-no-virtualenvs.patch
6f4235d0ab591d4fb3ea9327199a5149821f20d05aebe21c01430d1ac793bb3083f13884db1ac670dd51c074e6d769672fab27001392213f7b997bb0b5187493  43-LogClock.h.patch
545bcfd907937029e316487bd5cfe96aba2ca4dd1aa09d0c7901662a35c4bd63a71aa83773fdd05ab1889e5f73c47382fedcafbd90c371b01213f028d7136973  44-aarch64-erasure.patch
624ff72debfb698d681b187c99da9e927c6e4f5df7bc741b7dd196a9ee20773ac8a0f103314983faa29b934e6753407a02abfec2553feb1c3e1fcf20772cb1d9  44-cmake-buildtype.patch
f89c913a53e2806c59508d26f5dc72abb428c7e4e3b7c1aeb6eaf92744ea9a13cac2f00a2ac90f91ad7682f66d876a5fffedd10feeceecd71a944793581da443  44-staticcast.patch
b95ec157f9c77177afd3deb8e3485bb5f10e1b634a15617d14e09b08d6680d32201ecb249ea5ea98127a312e1fdb22fdbe9f0216690291a2849419aa7ab81610  44-missing-include.patch
fd0cbe188a1be404c1d6f221367b94d764e78cf61e7ba4e8a8977ebabd467356a3a46e079df3e5dfa3fd301dd7991bd0f2eabca2e9cd28e5edcae24fb9b8456c  45-python.patch
"
